//
//  CommonView.m
//  FourthProject
//
//  Created by mac on 16/5/26.
//  Copyright © 2016年 cry. All rights reserved.
//

#import "CommonView.h"
#import "BookCollectionViewCell.h"
#import "BookObject.h"
#import "RecipeCollectionReusableView.h"

@implementation CommonView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
        float w = (WIDTH - 4 * 10) / 3;
        float h = (HEIGHT - 50 - 40 - 3 * 10) / 2;
        fl.itemSize = CGSizeMake(w ,h);
        fl.sectionInset = UIEdgeInsetsMake(20, 0, 10, 0);
        
        self.myCollect = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:fl];
        
        self.myCollect.delegate = self;
        self.myCollect.dataSource = self;
        self.myCollect.backgroundColor = [UIColor clearColor];
        [self addSubview:self.myCollect];

        [self.myCollect registerNib:[UINib nibWithNibName:@"BookCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
        
        [self.myCollect registerNib:[UINib nibWithNibName:@"RecipeCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"hhh"];
        self.myCollect.allowsMultipleSelection = NO;
        
    }
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.countryArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.countryArray[section][@"guides"] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BookObject *obj = self.countryArray[indexPath.section][@"guides"][indexPath.row];
    
    BookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",obj.cover]]];
    cell.chineseLabel.text = obj.guide_cnname;
    cell.englishLabel.text = obj.guide_enname;
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    RecipeCollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"hhh" forIndexPath:indexPath];
    
    //UILabel *label = (UILabel *)[reuseableView viewWithTag:1];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        reuseableView.backgroundColor = [UIColor cyanColor];
        reuseableView.headerViewLabel.text = self.countryArray[indexPath.section][@"name"];
//        label.text = self.countryArray[indexPath.section][@"name"];
        
        return reuseableView;
    }
   
    return nil;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(WIDTH, 30);
}


@end
